package com.busi.wms.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.busi.wms.model.WmsInstorage;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Record;
import com.zcurd.common.tool.DBTool;
import com.zcurd.common.util.StringUtil;
import com.zcurd.controller.BaseController;
import com.zcurd.ext.render.csv.CsvRender;

/**
 * 系统入库功能
 */
public class WmsInstorageController extends BaseController {
	
	public void listPage() {
		setAttr("dictDatain_type", WmsInstorage.me.getDictDatain_type());
		setAttr("dictDatastorage_no", WmsInstorage.me.getDictDatastorage_no());
		setAttr("dictDataoperate_type", WmsInstorage.me.getDictDataoperate_type());
		setAttr("dictDatain_status", WmsInstorage.me.getDictDatain_status());
		render("list.html");
	}
	
	public void listData() {
		Object[] queryParams = getQueryParams();
		String[] properties = (String[]) queryParams[0];
		String[] symbols = (String[]) queryParams[1];
		Object[] values = (Object[]) queryParams[2];
		
		String orderBy = getOrderBy();
		if(StringUtil.isEmpty(orderBy)) {
			orderBy = "id desc";
		}
		
		List<Record> list = DBTool.findByMultProperties("wms_instorage", properties, symbols, values, orderBy, getPager());
		Map<String, Object> dictDatain_type = WmsInstorage.me.getDictDatain_type();
		for(Record record : list) {
			String fieldName = "in_type";
			if(dictDatain_type.get(record.get(fieldName).toString()) != null) {
				record.set(fieldName, dictDatain_type.get(record.get(fieldName).toString()));
			}
		}
		Map<String, Object> dictDatastorage_no = WmsInstorage.me.getDictDatastorage_no();
		for(Record record : list) {
			String fieldName = "storage_no";
			if(dictDatastorage_no.get(record.get(fieldName).toString()) != null) {
				record.set(fieldName, dictDatastorage_no.get(record.get(fieldName).toString()));
			}
		}
		Map<String, Object> dictDataoperate_type = WmsInstorage.me.getDictDataoperate_type();
		for(Record record : list) {
			String fieldName = "operate_type";
			if(dictDataoperate_type.get(record.get(fieldName).toString()) != null) {
				record.set(fieldName, dictDataoperate_type.get(record.get(fieldName).toString()));
			}
		}
		Map<String, Object> dictDatain_status = WmsInstorage.me.getDictDatain_status();
		for(Record record : list) {
			String fieldName = "in_status";
			if(dictDatain_status.get(record.get(fieldName).toString()) != null) {
				record.set(fieldName, dictDatain_status.get(record.get(fieldName).toString()));
			}
		}
		renderDatagrid(
			list, 
			DBTool.countByMultProperties("wms_instorage", properties, symbols, values)
		);
	}
	
	//增加页面
	public void addPage() {
		setAttr("dictDatain_type", WmsInstorage.me.getDictDatain_type());
		setAttr("dictDatastorage_no", WmsInstorage.me.getDictDatastorage_no());
		setAttr("dictDataoperate_type", WmsInstorage.me.getDictDataoperate_type());
		//WmsInstorage wmsi= new WmsInstorage();
		setAttr("in_name",PropKit.get("comp_name")+"入库单");
		render("add.html");
	}
	
	//增加入库单
	public void add() {
		getModel(WmsInstorage.class, "model").save();
		
		addOpLog("[入库单主表] 增加");
		renderSuccess();
	}
	
	//修改页面
	public void updatePage() {
		setAttr("dictDatain_type", WmsInstorage.me.getDictDatain_type());
		setAttr("dictDatastorage_no", WmsInstorage.me.getDictDatastorage_no());
		setAttr("dictDataoperate_type", WmsInstorage.me.getDictDataoperate_type());
		setAttr("dictDatain_status", WmsInstorage.me.getDictDatain_status());
		setAttr("model", WmsInstorage.me.findById(getPara("id")));
		render("update.html");
	}
	//修改
	public void update() {
		WmsInstorage model = WmsInstorage.me.findById(getPara("id"));
		model.set("in_type", getPara("model.in_type"));
		model.set("order_no", getPara("model.order_no"));
		model.set("storage_no", getPara("model.storage_no"));
		model.set("sup_num", getPara("model.sup_num"));
		model.set("in_time", getPara("model.in_time"));
		model.set("create_user", getPara("model.create_user"));
		model.set("audit_user", getPara("model.audit_user"));
		model.set("audit_time", getPara("model.audit_time"));
		model.set("print_user", getPara("model.print_user"));
		model.set("print_time", getPara("model.print_time"));
		model.set("operate_type", getPara("model.operate_type"));
		model.set("in_no", getPara("model.in_no"));
		model.set("in_barcode", getPara("model.in_barcode"));
		model.set("in_name", getPara("model.in_name"));
		model.set("in_batch", getPara("model.in_batch"));		
		model.update();
		addOpLog("[入库单主表] 修改");
		renderSuccess();
	}
	//审核页面
	public void pubishPage() {
		WmsInstorage model = WmsInstorage.me.findById(getParaToInt("id"));
		Map<String, Object> dictDatain_type = WmsInstorage.me.getDictDatain_type();
		if(dictDatain_type.get(model.get("in_type").toString()) != null) {
			model.set("in_type", dictDatain_type.get(model.get("in_type").toString()));
		}
		
		Map<String, Object> dictDatastorage_no = WmsInstorage.me.getDictDatastorage_no();
		if(dictDatastorage_no.get(model.get("storage_no").toString()) != null) {
			model.set("storage_no", dictDatastorage_no.get(model.get("storage_no").toString()));
		}
		Map<String, Object> dictDataoperate_type = WmsInstorage.me.getDictDataoperate_type();
		if(dictDataoperate_type.get(model.get("operate_type").toString()) != null) {
			model.set("operate_type", dictDataoperate_type.get(model.get("operate_type").toString()));
		}
		setAttr("dictDatain_status", WmsInstorage.me.getDictDatain_status());
		setAttr("model",model);
		render("publish.html");
		}
	//修改
	public void publish() {
		WmsInstorage model = WmsInstorage.me.findById(getPara("id"));		
		model.set("audit_user", getPara("model.audit_user"));
		model.set("audit_time", getPara("model.audit_time"));			
		model.set("in_status", getPara("model.in_status"));
		model.update();
		addOpLog("[入库单主表] 发布");
		renderSuccess();
		}	
	
	//删除
	public void delete() {
		WmsInstorage.me.findById(getPara("id")).delete();
		
		addOpLog("[入库单主表] 删除");
		renderSuccess();
	}
	
	//详情页面
	public void detailPage() {
		WmsInstorage model = WmsInstorage.me.findById(getParaToInt("id"));
		Map<String, Object> dictDatain_type = WmsInstorage.me.getDictDatain_type();
		if(dictDatain_type.get(model.get("in_type").toString()) != null) {
			model.set("in_type", dictDatain_type.get(model.get("in_type").toString()));
		}
		Map<String, Object> dictDatastorage_no = WmsInstorage.me.getDictDatastorage_no();
		if(dictDatastorage_no.get(model.get("storage_no").toString()) != null) {
			model.set("storage_no", dictDatastorage_no.get(model.get("storage_no").toString()));
		}
		Map<String, Object> dictDataoperate_type = WmsInstorage.me.getDictDataoperate_type();
		if(dictDataoperate_type.get(model.get("operate_type").toString()) != null) {
			model.set("operate_type", dictDataoperate_type.get(model.get("operate_type").toString()));
		}
		Map<String, Object> dictDatain_status = WmsInstorage.me.getDictDatain_status();
		if(dictDatain_status.get(model.get("in_status").toString()) != null) {
			model.set("in_status", dictDatain_status.get(model.get("in_status").toString()));
		}
		setAttr("model", model);
		render("detail.html");
	}
	//执行入库
	public void exeuInStorage(){
		    //需要拼装入库单数据	，入库逻辑	
			//WmsInstorage model = WmsInstorage.me.findById(getPara("id"));
			renderSuccess("未接入PLC");	
	}
	//导出csv
	public void exportCsv() {
		Object[] queryParams = getQueryParams();
		String[] properties = (String[]) queryParams[0];
		String[] symbols = (String[]) queryParams[1];
		Object[] values = (Object[]) queryParams[2];
		
		String orderBy = getOrderBy();
		if(StringUtil.isEmpty(orderBy)) {
			orderBy = "id desc";
		}
		
		List<Record> list = DBTool.findByMultProperties("wms_instorage", properties, symbols, values);
		Map<String, Object> dictDatain_type = WmsInstorage.me.getDictDatain_type();
		for(Record record : list) {
			String fieldName = "in_type";
			if(dictDatain_type.get(record.get(fieldName).toString()) != null) {
				record.set(fieldName, dictDatain_type.get(record.get(fieldName).toString()));
			}
		}
		Map<String, Object> dictDatastorage_no = WmsInstorage.me.getDictDatastorage_no();
		for(Record record : list) {
			String fieldName = "storage_no";
			if(dictDatastorage_no.get(record.get(fieldName).toString()) != null) {
				record.set(fieldName, dictDatastorage_no.get(record.get(fieldName).toString()));
			}
		}
		Map<String, Object> dictDataoperate_type = WmsInstorage.me.getDictDataoperate_type();
		for(Record record : list) {
			String fieldName = "operate_type";
			if(dictDataoperate_type.get(record.get(fieldName).toString()) != null) {
				record.set(fieldName, dictDataoperate_type.get(record.get(fieldName).toString()));
			}
		}
		
		List<String> headers = new ArrayList<String>();
		List<String> clomuns = new ArrayList<String>();
		headers.add("入库类型");
		clomuns.add("in_type");
		headers.add("订单编号");
		clomuns.add("order_no");
		headers.add("仓库编号");
		clomuns.add("storage_no");
		headers.add("供应商编号");
		clomuns.add("sup_num");
		headers.add("入库时间");
		clomuns.add("in_time");
		headers.add("入库人员");
		clomuns.add("create_user");
		headers.add("审核人员");
		clomuns.add("audit_user");
		headers.add("审核时间");
		clomuns.add("audit_time");
		headers.add("打印人员");
		clomuns.add("print_user");
		headers.add("打印时间");
		clomuns.add("print_time");
		headers.add("操作类型");
		clomuns.add("operate_type");
		headers.add("入库单编号");
		clomuns.add("in_no");
		headers.add("入库单条码");
		clomuns.add("in_barcode");
		headers.add("入库单名称");
		clomuns.add("in_name");
		headers.add("批次");
		clomuns.add("in_batch");
		
		CsvRender csvRender = new CsvRender(headers, list);
		csvRender.clomuns(clomuns);
		csvRender.fileName("入库单主表");
		
		addOpLog("[入库单主表] 导出cvs");
		render(csvRender);
	}	
}
